/*
 * @lc app=leetcode.cn id=150 lang=java
 *
 * [150] 逆波兰表达式求值
 */

import java.util.Stack;
 
// @lc code=start
class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();

        for(String str : tokens) {
            if(str.equals("+")) {
                stack.push(stack.pop() + stack.pop());
            } else if(str.equals("-")) {
                stack.push(-stack.pop() + stack.pop());
            } else if(str.equals("*")) {
                stack.push(stack.pop() * stack.pop());
            } else if(str.equals("/")) {
                int num = stack.pop();
                stack.push(stack.pop() / num);
            } else {
                stack.push(Integer.parseInt(str));
            }
        }
        
        return stack.pop();
    }
}
// @lc code=end

